package com.yaorange.teaching.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.yaorange.common.core.utils.DateUtils;
import com.yaorange.common.security.utils.SecurityUtils;
import com.yaorange.teaching.domain.YaCourseDesign;
import com.yaorange.teaching.domain.YaCourseDesignDetail;
import com.yaorange.teaching.mapper.YaCourseDesignDetailMapper;
import com.yaorange.teaching.service.IYaCourseDesignDetailService;
import com.yaorange.teaching.service.IYaCourseDesignService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.teaching.mapper.YaCourseMapper;
import com.yaorange.teaching.domain.YaCourse;
import com.yaorange.teaching.service.IYaCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
public class YaCourseServiceImpl extends ServiceImpl<YaCourseMapper,YaCourse> implements IYaCourseService
{
    @Autowired
    private YaCourseMapper yaCourseMapper;
    @Autowired
    private YaCourseDesignDetailMapper yaCourseDesignDetailMapper;
    @Autowired
    private IYaCourseDesignDetailService yaCourseDesignDetailService;
    /**
     * 查询课程
     *
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public YaCourse selectYaCourseById(Long id)
    {
        return yaCourseMapper.selectYaCourseById(id);
    }

    /**
     * 查询课程列表
     *
     * @param yaCourse 课程
     * @return 课程
     */
    @Override
    public List<YaCourse> selectYaCourseList(YaCourse yaCourse)
    {
        return yaCourseMapper.selectYaCourseList(yaCourse);
    }

    /**
     * 新增课程
     *
     * @param yaCourse 课程
     * @return 结果
     */
    @Override
    public int insertYaCourse(YaCourse yaCourse)
    {
        yaCourse.setCreateTime(DateUtils.getNowDate());
        return yaCourseMapper.insertYaCourse(yaCourse);
    }

    /**
     * 修改课程
     *
     * @param yaCourse 课程
     * @return 结果
     */
    @Override
    public int updateYaCourse(YaCourse yaCourse)
    {
        yaCourse.setUpdateTime(DateUtils.getNowDate());
        return yaCourseMapper.updateYaCourse(yaCourse);
    }

    /**
     * 批量删除课程
     *
     * @param ids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseByIds(Long[] ids)
    {
        return yaCourseMapper.deleteYaCourseByIds(ids);
    }

    /**
     * 删除课程信息
     *
     * @param id 课程主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseById(Long id)
    {
        return yaCourseMapper.deleteYaCourseById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYaCourseDesignDetail(Integer stage, Long courseId, List<YaCourseDesignDetail> courseDesignDetailList) {
        //考虑到三种 情况，1.新增，2.修改，3.删除
        //1.先把需要删除的数据删除(避免删除掉新增的)
        List<Long> idsList = courseDesignDetailList.stream().filter(item-> ObjectUtils.isNotEmpty(item.getId())).map(YaCourseDesignDetail::getId).collect(Collectors.toList());
        yaCourseDesignDetailMapper.deleteYaCourseDesignDetailByIdsNotIn(stage,courseId,idsList);
        //2.新增或者修改
        List<YaCourseDesignDetail> updateList = new ArrayList<>(courseDesignDetailList.size());
        List<YaCourseDesignDetail> addList = new ArrayList<>(courseDesignDetailList.size());
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        courseDesignDetailList.forEach(cd->{
            //判断是否有id
            if (ObjectUtils.isEmpty(cd.getId())) {
                cd.setStage(stage);
                cd.setCourseId(courseId);
                cd.setCreateTime(nowDate);
                cd.setCreateBy(username);
                addList.add(cd);
            } else {
                cd.setUpdateTime(nowDate);
                cd.setUpdateBy(username);
                updateList.add(cd);
            }
        });
        yaCourseDesignDetailService.saveBatch(addList,100);
        yaCourseDesignDetailService.updateBatchById(updateList,100);
        return 1;
    }

}
